<template>
    <!-- 表单部分 -->
    <el-form inline>
      <el-form-item label="子分类名称：">
        <el-input v-model="queryForm.name" clearable />
      </el-form-item>
      <el-form-item label="分类">
        <CategorySelect v-model="queryForm.categoryId" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getSubCategoryPage">
          <el-icon><Search /></el-icon>搜索
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addSubCategory">+新增分类</el-button>
        <el-button type="danger" @click="deleBatch">批量删除</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格展示 -->
    <el-table
      ref="tableRef"
      v-loading="loading"
      :data="subCategoryList"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column label="图片">
        <template #default="scope">
          <img
            :src="scope.row.picture"
            alt="图片"
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="子分类名称" />
      <el-table-column prop="categoryName" label="分类名称" />
      <el-table-column prop="updateTime" label="更新时间" />
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button type="primary" @click="editSubCategory(row)">
            <el-icon><Edit /></el-icon>
          </el-button>
          <el-popconfirm
            title="确认删除吗?"
            confirm-button-text="确认"
            cancel-button-text="取消"
            @confirm="deleSubCategory(row.id)"
          >
            <template #reference>
              <el-button type="danger">
                <el-icon><Delete /></el-icon>
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="queryForm.page"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[2, 5, 10, 20]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="getSubCategoryPage"
      @current-change="getSubCategoryPage"
    />

    <!-- 子分类编辑对话框 -->
    <SubCategoryEdit ref="dialog" @success="onSuccess" />
</template>

<script setup>
// 引入依赖
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import {
  getSubCategoryPageAPI,
  deleSubCategoryAPI,
} from "@/apis/category.js";
import SubCategoryEdit from "./components/SubCategoryEdit.vue";
import CategorySelect from "./components/CategorySelect.vue";

// 定义响应式数据
const queryForm = ref({
  page: 1,
  pageSize: 10,
  name: "",
  categoryId: "",
});

const subCategoryList = ref([]);
const loading = ref(false);
const total = ref(0);
const dialog = ref(null);
const tableRef = ref(null);

// 定义方法
const getSubCategoryPage = async () => {
  loading.value = true;
  const res = await getSubCategoryPageAPI(queryForm.value);
  subCategoryList.value = res.data.records;
  total.value = res.data.total;
  loading.value = false;
};

const addSubCategory = () => {
  dialog.value.open({});
};

const deleSubCategory = async (id) => {
  const res = await deleSubCategoryAPI(id);
  if (res.code === 1) {
    ElMessage.success("删除成功");
    getSubCategoryPage();
  }
};

const editSubCategory = (row) => {
  dialog.value.open(row);
};

const deleBatch = () => {
  const selectedRows = tableRef.value.getSelectionRows();
  if (selectedRows.length === 0) {
    ElMessage.warning("没有选中的子分类");
  } else {
    const selectedIds = selectedRows.map((row) => row.id).join(",");
    deleSubCategory(selectedIds);
  }
};

const onSuccess = async (isEdit) => {
  if (!isEdit) {
    const res = await getSubCategoryPageAPI(queryForm.value);
    const totalPages = Math.ceil(res.data.total / queryForm.value.pageSize);
    queryForm.value.page = totalPages;
  }
  getSubCategoryPage();
};
// 生命周期钩子
onMounted(() => {
  getSubCategoryPage();
});
</script>